package com.ruoyi.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AddGroup;
import com.ruoyi.system.domain.AddWechat;
import com.ruoyi.system.domain.Favorites;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.AddGroupService;
import com.ruoyi.system.service.AddWechatService;
import com.ruoyi.system.service.FavoritesService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

import static javax.management.Query.eq;

/*
 *  @author 雨安
 *  类名： FavoritesController
 *  创建时间：2025/2/11
 */
@RestController
@RequestMapping("/Favorites")
public class FavoritesController {

    @Resource
    private FavoritesService favoritesService;

    //查询
    @PostMapping("/get/Favorites")
    public AjaxResult getFavorites(@RequestBody Favorites favorites){
        //创建分页对象
        Page<Favorites> page = new Page<>();
        //设置页码
        page.setCurrent(favorites.getCurrent());
        //设置行数
        page.setSize(favorites.getSize());
        //执行查询
        favoritesService.page(page,new LambdaQueryWrapper<Favorites>()

                //用户id查找
                .eq(Objects.nonNull(favorites.getUserId()),Favorites::getUserId,favorites.getUserId())
                //appid查找（add_wechat id查找）
                .eq(Objects.nonNull(favorites.getAddId()),Favorites::getAddId,favorites.getAddId())
                //逻辑删除
                .eq(Favorites::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Favorites::getCreateTime)
                //添加分页语句
                .last(" limit " + (favorites.getCurrent() - 1) * favorites.getSize() +"," + favorites.getSize())

        );
        page.setTotal(favoritesService.count(new LambdaQueryWrapper<Favorites>()
                //用户id查找
                .eq(Objects.nonNull(favorites.getUserId()),Favorites::getUserId,favorites.getUserId())
                //appid查找（add_wechat id查找）
                .eq(Objects.nonNull(favorites.getAddId()),Favorites::getAddId,favorites.getAddId())
                //逻辑删除
                .eq(Favorites::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Favorites::getCreateTime)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    //删除
    @PostMapping("/delete/Favorites/{id}")
    public AjaxResult deleteFavorites(@PathVariable(value = "id")Long id){
        //调用Service层删除
        favoritesService.update(null,new UpdateWrapper<Favorites>().lambda()
                //主键id
                .eq(Favorites::getId,id)
                //设置逻辑删除为 真
                .set(Favorites::getDeleteFlag,Boolean.TRUE)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }
    
    //增加
    @PostMapping("/insert/Favorites")
    public AjaxResult insertFavorites(@RequestBody Favorites favorites){
        //设置默认值
        favorites.setCreateBy(SecurityUtils.getUsername());
        favorites.setCreateTime(new Date());
        favorites.setUpdateBy(SecurityUtils.getUsername());
        favorites.setUpdateTime(new Date());
        favorites.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        favoritesService.save(favorites);
        //返回结果
        return AjaxResult.success("新增成功");
    }
    //用户表
    @Resource
    private SysUserMapper sysUserMapper;
    //修改
    @PostMapping("/update/Favorites")
    public AjaxResult updateFavorites(@RequestBody Favorites favorites){
        //设置默认值
        favorites.setUpdateTime(new Date());
        favorites.setDeleteFlag(Boolean.FALSE);
        //调用Service层
        favoritesService.updateById(favorites);
        //返回结果
        return AjaxResult.success("修改成功");
    }

    //添加收藏
    @PostMapping("/update/favoritesUi")
    public AjaxResult addFavorites(@RequestBody Favorites favorites){
        //获取用户
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        //查询收藏表
        Favorites actived = favoritesService.getOne(new LambdaQueryWrapper<Favorites>()
                //用户id
                .eq(Favorites::getUserId, SecurityUtils.getUserId())
                //收藏的addid
                .eq(Favorites::getAddId, favorites.getAddId())
                //逻辑删除
                .eq(Favorites::getDeleteFlag, Boolean.FALSE)
                //查询一条即可
                .last("limit 1")
        );
        //判断是否已经收藏过该对象
        if (Objects.nonNull(actived)){
            //返回结果
            return AjaxResult.error("用户已收藏该对象");
        }
        favorites.setCreateBy(SecurityUtils.getUsername());
        QueryWrapper<Favorites> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(sysUser.getUserId()),"user_id",sysUser.getUserId())
                .eq(Objects.nonNull(favorites.getAddId()),"add_id",favorites.getAddId())
                .eq("delete_flag",Boolean.FALSE);
        long count = favoritesService.count(queryWrapper);
        if (count == 0){
            favorites.setUserId(sysUser.getUserId());
            //设置默认值
            favorites.setCreateBy(SecurityUtils.getUsername());
            favorites.setCreateTime(new Date());
            favorites.setUpdateBy(SecurityUtils.getUsername());
            favorites.setUpdateTime(new Date());
            favorites.setDeleteFlag(Boolean.FALSE);
            //调用Service层
            favoritesService.save(favorites);
        }
        //返回结果
        return AjaxResult.success("收藏成功");
    }

@Resource

private AddWechatService addWechatService;

    //查询用户收藏表数据到list
    @PostMapping("/get/Favorites/userData")
    public AjaxResult getFavoritesUserData(@RequestBody Favorites favorites){
        //创建分页对象
        Page<Favorites> page = new Page<>();
        //设置页码
        page.setCurrent(favorites.getCurrent());
        //设置行数
        page.setSize(favorites.getSize());
        //执行查询
        favoritesService.page(page,new LambdaQueryWrapper<Favorites>()
                //用户id查找
                .eq(Favorites::getUserId,SecurityUtils.getUserId())
                //逻辑删除
                .eq(Favorites::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Favorites::getCreateTime)
                //添加分页语句
                .last(" limit " + (favorites.getCurrent() - 1) * favorites.getSize() +"," + favorites.getSize())

        );
        page.setTotal(favoritesService.count(new LambdaQueryWrapper<Favorites>()
                //用户id查找
                .eq(Favorites::getUserId,SecurityUtils.getUserId())
                //逻辑删除
                .eq(Favorites::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Favorites::getCreateTime)
                //添加分页语句
                .orderByDesc(Favorites::getCreateTime)
        ));
        page.getRecords().stream().forEach(ele->{
            //判断是否有任务id
            if (Objects.nonNull(ele.getAddId())){
                //查询任务
                AddWechat addWechat = addWechatService.getById(ele.getAddId());
                //判断查询对象不为 null
                if (Objects.nonNull(addWechat)){
                    //设置对象给遍历对象
                    ele.setAddWechat(addWechat);
                }
            }
        });
        //返回结果
        return AjaxResult.success(page);
    }

    //删除
    @PostMapping("/delete/Favorites/QuXiaoSC")
    public AjaxResult deleteFavoritesQuXiaoSC(@RequestBody Favorites favorites){
        //调用Service层删除
        favoritesService.update(null,new UpdateWrapper<Favorites>().lambda()
                //主键id
                .eq(Favorites::getId,favorites.getId())
                //设置逻辑删除为 真
                .set(Favorites::getDeleteFlag,Boolean.TRUE)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }
}